package movEat.GUI;

import movEat.Definitions.Decade;
import movEat.Definitions.TVshowDB;
import movEat.Kernel.Import.Utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class TvShowParametersComposite {

	private Composite Composite;
	private Text tvShowName;
	private Text tvShowStars;
	private Combo tvShowDecade;

	public TvShowParametersComposite(Composite father, boolean dontKnowDecadeEnable){
		Composite = new Composite(father, SWT.NONE);
		Composite.setLayout(new GridLayout(1,false));
		Composite.setLayoutData(new GridData(SWT.NONE,SWT.NONE,false,false,SWT.FILL,SWT.NONE));

		Composite editTvShow_Params = new Composite(Composite, SWT.NONE);
		editTvShow_Params.setLayout(new RowLayout());
		editTvShow_Params.setLayoutData(new GridData(SWT.FILL,SWT.NONE,false,false,SWT.NONE,SWT.NONE));

		Label tvShowNameLabel = new Label(editTvShow_Params, SWT.NONE);
		tvShowNameLabel.setText("The Tv Show name: ");
		tvShowNameLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		tvShowName = new Text(editTvShow_Params, SWT.BORDER);
		tvShowName.setText("The Tv Show name");
		tvShowName.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		tvShowName.addFocusListener(new FocusAdapter(){
			@Override
			public void focusGained(FocusEvent e) {
				if(tvShowName.getText().equals("The Tv Show name"))
					tvShowName.setText("");
			}
			@Override
			public void focusLost(FocusEvent e)  {
				if(tvShowName.getText().equals(""))
					tvShowName.setText("The Tv Show name");
			}
		});

		Label tvShowPriceLabel = new Label(editTvShow_Params, SWT.NONE);
		tvShowPriceLabel.setText("The Tv Show price: ");
		tvShowPriceLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		tvShowStars = new Text(editTvShow_Params, SWT.BORDER);
		tvShowStars.setText("The Tv Show price (0-100 $)");
		tvShowStars.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));
		tvShowStars.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){
				e.doit = false; // don't allow anything but numbers and backspace
				if(Character.isDigit(e.character) || e.keyCode==SWT.BS || e.keyCode==SWT.DEL) {
					e.doit = true;
				}
			}
		});

		tvShowStars.addFocusListener(new FocusAdapter(){
			@Override
			public void focusGained(FocusEvent e) {
				if(tvShowStars.getText().equals("The Tv Show price (0-100 $)"))
					tvShowStars.setText("");
			}
			@Override
			public void focusLost(FocusEvent e)  {
				if(tvShowStars.getText().equals(""))
					tvShowStars.setText("The Tv Show price (0-100 $)");
			}
		});

		Label tvShowDecadeLabel = new Label(editTvShow_Params, SWT.NONE);
		tvShowDecadeLabel.setText("The Tv Show decade: ");
		tvShowDecadeLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		tvShowDecade = new Combo(editTvShow_Params, SWT.DROP_DOWN);

		for (Decade decade : Decade.values()) {
			tvShowDecade.add(decade.toString());
		}

		if (dontKnowDecadeEnable)
			tvShowDecade.add("Don't Know");

		tvShowDecade.setText("Decade of the Tv Show");
		tvShowDecade.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));
		tvShowDecade.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){
				e.doit = false; // don't allow anything

			}
		});

	}

	public void setEnabled(boolean b) {
		Composite.setEnabled(b);


	}

	public void setName(String imdbName) {
		tvShowName.setText(imdbName);
	}

	public void setStars(String stars) {
		tvShowStars.setText(stars);

	}

	public void setDecade(String rating) {
		tvShowDecade.setText(rating);

	}

	public void setDecadeEnable(boolean b) {
		tvShowDecade.setEnabled(b);

	}

	private String getName() {
		return tvShowName.getText();
	}

	private String getStars() {
		return tvShowStars.getText();
	}

	private String getDecade() {
		return tvShowDecade.getText();
		//		String dec =  tvShowDecade.getText();
		//		if (dec.equals("Decade of the Tv Show")||dec.equals("Don't Know"))
		//				return "";
		//		else 
		//			return dec;
	}

	public TVshowDB getTvShowFromUserInput_find() {

		String error = varifyUserInput_findTvShows();

		if (!error.equals("")){
			GUI.userMessage(GUI.shell,"Not A vald Input",error);
			return null;
		}


		TVshowDB tvShow = new TVshowDB();

		tvShow.setImdbName(getName());

		String stars = getStars();
		if(stars.equals("") || stars.equals("The Tv Show price (0-100 $)"))
			tvShow.setStars(-1);
		else
			tvShow.setStars(Integer.valueOf(stars));

		String decade = getDecade(); 
		if(decade.equals("Don't Know") || decade.equals("Decade of the Tv Show"))
			tvShow.setDecade(null);
		else
			tvShow.setDecade(Decade.valueOf(decade));
		return tvShow;
	}

	private String varifyUserInput_findTvShows() {

		String error = "";

		if (getName().equals("") || getName().equals("The Tv Show name"))
			error += "please type a Tv Show name or a part of it.\n";

		if(getName().indexOf('_')!=-1 || getName().indexOf('%')!=-1)
			error += "The name can not contain the chars '%' and '_'";
		
		if (!Utils.isValidAscii(getName()))
			error += "please write the name in Ascii (english for example).\n";
		
		if(!getStars().equals("The Tv Show price (0-100 $)") && !getStars().equals("")){
			double stars = Double.valueOf(getStars());
			if (stars < 0 || stars > 100)
				error += "The price must be in the range 0-100 $.\n";
		}

		return error;

	}

	public TVshowDB getTvShowFromUserInput_edit() {
		String error = varifyUserInput_editTvShow();

		if (!error.equals("")){
			if(GUI.subShell != null && !GUI.subShell.isDisposed())
				GUI.userMessage(GUI.subShell,"Not a valid Input",error);
			else
				GUI.userMessage(GUI.shell,"Not a valid Input",error);
			return null;
		}

		TVshowDB tvShow = new TVshowDB();

		tvShow.setImdbName(getName());

		String stars = getStars();
		tvShow.setStars(Integer.valueOf(stars));

		String decade = getDecade();
		tvShow.setDecade(Decade.valueOf(decade));
		return tvShow;

	}

	private String varifyUserInput_editTvShow() {
		String error = "";

		if (getName().equals("") )
			error += "please type a Name for the TV Show.\n";

//		if(getName().indexOf('_')!=-1 || getName().indexOf('%')!=-1)
//			error += "The name can not contain the chars '%' and '_'";
		
		if (!Utils.isValidAscii(getName()))
			error += "please write the name in Ascii (english for example).\n";
		
		if(getStars().equals(""))
			error += "Please select a price (0-100 $) for the movie.\n";

		else
		{
			int stars = Integer.valueOf(getStars());
			if (stars < 0 || stars > 100)
				error += "The Price be in the range 0-100 $.\n";
		}

		//		String decade = getDecade(); 
		//		if(decade.equals("") || decade.equals("The Tv Show Decade"))
		//			error += "decade must be one of the members in the list of decades.\n";

		return error;
	}

	public TVshowDB getTvShowFromUserInput_add() {
		String error = verifyUserInput_addTvShow();

		if (!error.equals("")){
			GUI.userMessage(GUI.shell,"Not a valid Input",error);
			return null;
		}

		TVshowDB tvShow = new TVshowDB();

		tvShow.setImdbName(getName());

		String stars = getStars();
		tvShow.setStars(Integer.valueOf(stars));

		String decade = getDecade();
		tvShow.setDecade(Decade.valueOf(decade));
		return tvShow;

	}

	private String verifyUserInput_addTvShow() {
		String error = "";

		if (getName().equals("") ||getName().equals("The Tv Show name"))
			error += "please type a Name for the TV Show.\n";

//		if(getName().indexOf('_')!=-1 || getName().indexOf('%')!=-1)
//			error += "The name can not contain the chars '%' and '_'";
				
		if (!Utils.isValidAscii(getName()))
			error += "please write in Ascii (english for example).\n";
		
		if(getStars().equals("")||getStars().equals("The Tv Show price (0-100 $)"))
			error += "Please select a price (0-100 $) for the movie.\n";

		else
		{
			int stars = Integer.valueOf(getStars());
			if (stars < 0 || stars > 100)
				error += "The Price be in the range 0-100 $.\n";
		}

		String decade = getDecade(); 

		if(decade.equals("Don't Know") || decade.equals("Decade of the Tv Show"))
			error += "decade must be one of the members in the list of decades.\n";

		return error;
	}



}

